package com.carey.blog.action.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.action.util.Filler;
import com.carey.blog.framework.latke.action.AbstractCacheablePageAction;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.model.Pagination;
import com.carey.blog.framework.latke.service.LangPropsService;
import com.carey.blog.framework.latke.util.Locales;
import com.carey.blog.model.Common;
import com.carey.blog.model.Preference;
import com.carey.blog.model.Skin;
import com.carey.blog.util.Preferences;

/**
 * Index action. index.ftl.
 * 
 */
public final class IndexAction extends AbstractCacheablePageAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(IndexAction.class
			.getName());
	/**
	 * Language service.
	 */
	private LangPropsService langPropsService = LangPropsService.getInstance();
	/**
	 * Filler.
	 */
	private Filler filler = Filler.getInstance();
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final Map<String, Object> ret = new HashMap<String, Object>();

		try {
			final JSONObject queryStringJSONObject = getQueryStringJSONObject(request);
			final int currentPageNum = queryStringJSONObject.optInt(
					Pagination.PAGINATION_CURRENT_PAGE_NUM, 1);

			final JSONObject preference = preferenceUtils.getPreference();
			if (null == preference) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return ret;
			}

			final String localeString = preference
					.getString(Preference.LOCALE_STRING);
			final Locale locale = new Locale(Locales.getLanguage(localeString),
					Locales.getCountry(localeString));

			final Map<String, String> langs = langPropsService.getAll(locale);
			ret.putAll(langs);

			if ("valentine".equals(preference.getString(Skin.SKIN_DIR_NAME))) {
				final int leftCurrentPageNum = queryStringJSONObject.optInt(
						Pagination.PAGINATION_CURRENT_PAGE_NUM
								+ Common.LEFT_PART_NAME, 1);
				final int rightCurrentPageNum = queryStringJSONObject.optInt(
						Pagination.PAGINATION_CURRENT_PAGE_NUM
								+ Common.RIGHT_PART_NAME, 1);
				filler.fillIndexArticlesForValentine(ret, leftCurrentPageNum,
						rightCurrentPageNum, preference);
			} else {
				filler.fillIndexArticles(ret, currentPageNum, preference);
			}
			
			filler.fillSide(ret, preference);
			filler.fillBlogHeader(ret, preference);
			filler.fillBlogFooter(ret, preference);
			ret.put(Common.ACTION_NAME, Common.INDEX);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);

			try {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);

				return ret;
			} catch (final IOException ex) {
				LOGGER.severe(ex.getMessage());
			}
		}

		return ret;
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
